﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VelocityShop;
using VelocityShop.Entities;

namespace VelocityShop.Windows.UI
{
    public partial class Form1 : Form
    {
        private Catalog _catalog;
        private IList<CatalogItem> _items;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnGetCatalog_Click(object sender, EventArgs e)
        {
            
            _catalog = CatalogManager.GetCatalog(); 
            _items = CatalogManager.GetItemsByCategory("TV"); 

            CatalogItemBindingSource.DataSource = from item in _items orderby item.ProductId select item;
            lblCurrency.Text = _catalog.Currency;
            UpdateItemCount();

        }

        private void btnAddToCatalog_Click(object sender, EventArgs e)
        {
            CatalogItem item = new CatalogItem { ProductId = productIdTextBox.Text,  
                                                 Description = descriptionTextBox.Text, 
                                                 UnitPrice = decimal.Parse( unitPriceTextBox.Text),
                                                 UnitsInStock = int.Parse(unitsInStockTextBox.Text),
                                                 ImageFileName = "TV_ProductImage1.jpg"
            };

            CatalogManager.InsertCatalogItem("TV", item);

            CatalogItemBindingSource.Add(item);
            CatalogItemBindingSource.MoveLast();
 
            _catalog.LastUpdate = DateTime.Now;
            UpdateItemCount();
            
        }

        private void UpdateItemCount()
        {
            lblcount.Text = string.Format("Catalog created at:{0} items in catalog: {1} loaded at: {2} updated at:{3}", _items.Count.ToString(), 
                                                                                                                        _catalog.Id,
                                                                                                                        _catalog.CreationDate.ToLongTimeString(),
                                                                                                                        _catalog.LastUpdate.ToLongTimeString());
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void CatalogItemBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }


     }
}
